package com.dataaccess.dataaccess.support.coap;

import com.dataaccess.dataaccess.model.CoapDeviceDataChanged;
import com.dataaccess.dataaccess.web.mapper.CoapDeviceDataChangedMapper;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@Component
public class MyCoapServer {

    @Resource
    private CoapDeviceDataChangedMapper coapDeviceDataChangedMapper;

    public void getCoapServer() throws IOException {
        CoapServer server = new CoapServer(5683);//主机为localhost 端口为默认端口5683
        server.add(new CoapResource("data"){//创建一个资源为hello 请求格式为 主机：端口\hello

            @Override
            public void handleGET(CoapExchange exchange) { //重写处理GET请求的方法
                System.out.println("getCoapServer get start");
                String result = exchange.getRequestText();
                exchange.respond(CoAP.ResponseCode.CONTENT, result);
                //存储数据
                CoapDeviceDataChanged coapDeviceDataChanged = new CoapDeviceDataChanged();
                coapDeviceDataChanged.setId(UUID.randomUUID().toString());
                coapDeviceDataChanged.setJsondata(result);
                coapDeviceDataChangedMapper.insert(coapDeviceDataChanged);
            }
            @Override
            public void handlePOST(CoapExchange exchange) {
                System.out.println("getCoapServer post start");
//                String result = HexUtil.encodeHexStr(exchange.getRequestPayload());
//                System.out.println("the received text:"+result);
                //exchange.respond(ResponseCode.CHANGED);
                exchange.respond(CoAP.ResponseCode.CONTENT, exchange.getRequestText());
                //存储数据
                CoapDeviceDataChanged coapDeviceDataChanged = new CoapDeviceDataChanged();
                coapDeviceDataChanged.setId(UUID.randomUUID().toString());
                coapDeviceDataChanged.setJsondata(exchange.getRequestText());
                coapDeviceDataChangedMapper.insert(coapDeviceDataChanged);

            }

        });
        server.start();
//        ServerSocket serverSocket = new ServerSocket(5683);  //监听8800端口
//        while(true){
//            Socket socket = serverSocket.accept();//等待客户连接
//            InputStream socketIn = socket.getInputStream();
//            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socketIn));
//            String msg = null;
//            while ((msg=bufferedReader.readLine())!=null) {
//                CoapDeviceDataChanged coapDeviceDataChanged = new CoapDeviceDataChanged();
//                coapDeviceDataChanged.setId(UUID.randomUUID().toString());
//                coapDeviceDataChanged.setJsondata(msg);
//                coapDeviceDataChangedMapper.insert(coapDeviceDataChanged);
//            }
//        }
    }

    public static void main(String[] args) throws IOException {
//        com.dataaccess.dataaccess.support.coap.CoapServer server = new com.dataaccess.dataaccess.support.coap.CoapServer(5683);
//
//        server.add(new CoapResource("aa"){
//            @Override
//            public void handleGET(CoapExchange exchange) { //重写处理GET请求的方法
//                exchange.respond(CoAP.ResponseCode.CONTENT, exchange.getRequestText());
//            }
//            @Override
//            public void handlePOST(CoapExchange exchange) { //重写处理GET请求的方法
//                System.out.println(22222);
//                String result = HexUtil.encodeHexStr(exchange.getRequestPayload());
//                exchange.respond(CoAP.ResponseCode.CONTENT, result);
//            }
//
//        });
//        MyCoapResource coapResource = new MyCoapResource("");
//        coapResource.setName("data");
//        server.add(coapResource);
//        server.start();

//        DatagramSocket ds = new DatagramSocket(5683);
//while(true){
//    /**2、预先创建数据存放的位置，封装*/
//    byte [] bbuf = new byte [36];
//    DatagramPacket dp = new DatagramPacket(bbuf,bbuf.length);
//    byte[] tt = dp.getData();
//    /**3、使用receive阻塞式接收*/
//    ds.receive(dp);
//
//    System.out.println("ip::"+dp.getAddress().getHostAddress()+"\nport::"+dp.getPort()+"\ndata::"+new String(tt));
//}


    }
}
